#include "OSSManage.h"
#include <memory>

static string endpoint = "oss-cn-beijing.aliyuncs.com";
static string accessKeyId = "LTAI5t9catJnTjtwVSLmVgb7";
static string accessKeySercret = "PAY09SjR3PxXvf1Z3Vzc134wPNSslo";
static string region = "cn-beijing";
static string bucketName = "liin-iporsche";
static string YunRootPath = "disk/";

using AlibabaCloud::OSS::InitializeSdk;
using AlibabaCloud::OSS::ShutdownSdk;
using AlibabaCloud::OSS::PutObjectRequest;


OSSManage::OSSManage()
:conf()
,client(endpoint ,accessKeyId, accessKeySercret, conf)
{
    InitializeSdk();
    client.SetRegion(region);
}

OSSManage::~OSSManage()
{
    ShutdownSdk();
}

void OSSManage::OSSupload(string objectName , string & content)
{
    objectName = YunRootPath + objectName;
    std::shared_ptr<std::iostream> stream = std::make_shared<std::stringstream>(std::move(content));
    PutObjectRequest request {bucketName,objectName,stream};
    auto outcome = client.PutObject(request);
    disposeError(outcome);
    return;
}

void OSSManage::disposeError(const PutObjectOutcome & outcome)
{
    if (!outcome.isSuccess()) {
        /* 异常处理。*/
        std::cout << "PutObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
    }
    return;
}